package com.kk;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * @author : K k
 * @date : 16:46 2020/6/10
 */
public class CodeGenerator {

    public static void main(String[] args) {
        //1、构造一个生成器对象
        AutoGenerator mpg=new AutoGenerator();

        //2、配置全局策略  mpg.setGlobalConfig(gc)
        GlobalConfig gc=new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath+"/src/main/java");
        gc.setAuthor("kk");
        gc.setOpen(false);
        gc.setFileOverride(false);
        gc.setServiceName("%sService");//去I前缀
        gc.setIdType(IdType.ID_WORKER);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);

        //3、配置数据源 mpg.setDataSource(dsc);
        DataSourceConfig dsc=new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/vueblog?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("Believe");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        //4、包的配置 mpg.setPackageInfo(pc);
        PackageConfig pc=new PackageConfig();
        //pc.setModuleName("mybatis");
        pc.setParent("com.kk");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);

        //5、生成策略
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("m_user","m_blog");  //设置表名 可多个
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setTablePrefix("m_"); //表前缀
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        //逻辑删除
        strategy.setLogicDeleteFieldName("status");
        //自动填充
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills=new ArrayList<>();
        tableFills.add(createTime);tableFills.add(updateTime);
        strategy.setTableFillList(tableFills);
        //乐观锁
        strategy.setVersionFieldName("version");
        //驼峰命名
        strategy.setRestControllerStyle(true);
        mpg.setStrategy(strategy);

        //6、执行
        mpg.execute();
    }
}
